package com.yexiao.cofe.business.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.yexiao.cofe.business.mapper.PermissionMapper;
import com.yexiao.cofe.business.pojo.Permission;
import com.yexiao.cofe.business.service.PermissionService;
import com.yexiao.cofe.common.exception.BusinessException;
import com.yexiao.cofe.util.SystemUtil;
import com.yexiao.core.util.JSONUtil;
import com.yexiao.core.util.StrUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lkl
 * @since 2025/4/8
 */
@Service
public class PermissionServiceImpl implements PermissionService {

    @Resource
    private PermissionMapper permissionMapper;

    @Override
    public void addPermission(JSONObject param, String token) {
        String permissionId = JSONUtil.checkAndGetStr(param, "permissionId");
        String permissionName = JSONUtil.checkAndGetStr(param, "permissionName");
        String fatherPermissionId = JSONUtil.checkAndGetStr(param, "fatherPermissionId");
        Integer sort = JSONUtil.getIntDef(param, 0, "sort");

        Permission permission = permissionMapper.selectByPermissionId(permissionId);
        SystemUtil.checkErrorIfNotNull(permission, "权限ID重复");
        Permission fatherPermission = permissionMapper.selectByPermissionId(fatherPermissionId);
        SystemUtil.checkErrorIfNull(fatherPermission, "父权限不存在");


        Permission newPermission = new Permission();
        newPermission.setPermissionId(permissionId)
                .setPermissionName(permissionName)
                .setFatherPermissionId(fatherPermissionId)
                .setFatherPermissionName(fatherPermission.getFatherPermissionName())
                .setSort(sort);
        permissionMapper.insert(newPermission);
    }

    @Override
    public void deletePermission(String permissionId, String token) {
        if(StrUtil.isEmpty(permissionId)){
            throw new BusinessException("权限id为空");
        }

        if("root".equals(permissionId)) {
            throw new BusinessException("无法删除根节点");
        }

        // 检查是否有子权限
        int cnt = permissionMapper.countChildPermission(permissionId);
        if(cnt > 0){
            throw new BusinessException("请先删除子权限");
        }
        permissionMapper.delByPermissionId(permissionId);
    }

    @Override
    public void updatePermission(JSONObject param, String token) {
        Integer id = JSONUtil.checkAndGetInt(param, "id");
        String permissionId = JSONUtil.getStr(param, "permissionId");
        String permissionName = JSONUtil.getStr(param, "permissionName");
        String fatherPermissionId = JSONUtil.getStr(param, "fatherPermissionId");
        Integer sort = JSONUtil.getInt(param, "sort");

        Permission permission = permissionMapper.selectById(id);
        SystemUtil.checkErrorIfNull(permission, "权限不存在");


        String oldFatherPermissionId = "";
        if(StrUtil.isNotEmpty(permissionId) && StrUtil.notEquals(oldFatherPermissionId, permissionId)){
            // 新老id不同
            oldFatherPermissionId = permission.getPermissionId();
            permission.setPermissionId(permissionId);
            permission.setPermissionName(permissionName);
        }
        if(StrUtil.isNotEmpty(fatherPermissionId)) {
            Permission fatherPermission = permissionMapper.selectByPermissionId(fatherPermissionId);
            SystemUtil.checkErrorIfNull(fatherPermission, "父权限不存在");
            permission.setFatherPermissionId(fatherPermissionId);
            permission.setFatherPermissionName(fatherPermission.getFatherPermissionName());
        }
        if(sort != null) {
            permission.setSort(sort);
        }

        permissionMapper.updateById(permission);
        if(StrUtil.isNotEmpty(oldFatherPermissionId)) {
            // 更新子节点信息
            permissionMapper.updateChild(oldFatherPermissionId, permissionId, permissionName);
        }
    }

    @Override
    public JSONObject listAll(String token) {
        List<Permission> allPermission = permissionMapper.listAll();
        Permission root = null;
        for (Permission permission : allPermission) {
            // 找根节点
            if("".equals(permission.getFatherPermissionId())) {
                root = permission;
                break;
            }
        }
        if(root == null) {
            throw new BusinessException("根节点不存在");
        }

        JSONObject baseMsg = root.getBaseMsg();
        baseMsg.put("child", getChild(allPermission, root.getPermissionId()));
        return baseMsg;
    }

    /**
     * 获取某节点的子节点列表
     */
    private List<JSONObject> getChild(List<Permission> permissionList, String fatherPermissionId) {
        List<JSONObject> child = new ArrayList<>();
        for (Permission permission : permissionList) {
            if(fatherPermissionId.equals(permission.getFatherPermissionId())){
                JSONObject baseMsg = permission.getBaseMsg();
                baseMsg.put("child", getChild(permissionList, permission.getPermissionId()));
                child.add(baseMsg);
            }
        }
        return child;
    }
}
